package demo.hms.user.controller;

import demo.hms.entity.User;
import demo.hms.user.service.UserService;
import demo.hms.util.Result;
import demo.hms.util.ResultSubmit;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(@RequestBody Map<String,String> map, HttpServletRequest request) {
        System.out.println(1);
        String userId = map.get("userId");
        String password = map.get("userPassword");
        System.out.println(userId);
        System.out.println(password);
        if(userId == null || userId.equals("") || password == null || password.equals("")) {
            return ResultSubmit.error("登陆失败，输入参数不全");
        }
        return userService.login(userId, password, request);
    }

    @RequestMapping(value = "/login2",method = RequestMethod.GET)
    public Result login2(String userId,String password, HttpServletRequest request) {
        return userService.login(userId, password, request);
    }

    @RequestMapping(value = "/info")
    public Result info(HttpServletRequest request){
        return userService.info(request);
    }

    @RequestMapping(value = "/logout")
    public Result logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("userId",null);
        session.setAttribute("token",null);
        return ResultSubmit.success("退出登录成功");
    }

    @RequestMapping(value = "/selectAllUser")
    public Result selectAllUser(String userId,String userName,String roleName){
        return userService.selectAllUser(userId, userName, roleName);
    }

    @RequestMapping(value = "/insert")
    public Result insert(User user){
        return userService.insert(user);
    }

    @RequestMapping(value = "/update")
    public Result update(User user){
        return userService.update(user);
    }

    @RequestMapping(value = "/delete")
    public Result delete(String userId){
        return userService.delete(userId);
    }
}
